//
//  AEMyExchangeViewCell.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/14.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEMyExchangeViewCell.h"
#import "AEMyExchangeModel.h"
#import "AE_Solar-Swift.h"

@interface AEMyExchangeViewCell ()

@property (nonatomic, strong) AEMyExchangeModel *model;

@property (nonatomic, strong) UIView *bgView;

/// 状态
@property (nonatomic, strong) UILabel *statusLabel;

/// 订单号
@property (nonatomic, strong) UILabel *orderNoLabel;

/// 缩略图
@property (nonatomic, strong) UIImageView *thumalImgView;

/// 商品名称
@property (nonatomic, strong) UILabel *goodsNameLabel;

/// 积分
@property (nonatomic, strong) UILabel *cntLabel;

/// 备注
@property (nonatomic, strong) UILabel *remarkLabel;

@property (nonatomic, strong) CALayer *oneLine;

/// 下单时间
@property (nonatomic, strong) UILabel *createTimeLabel;

/// 收货方式
@property (nonatomic, strong) UILabel *receiveWayLabel;

/// 支付方式
@property (nonatomic, strong) UILabel *paymentLabel;


@property (nonatomic, strong) CALayer *twoLine;

/// 确认领取
@property (nonatomic, strong) UIButton *btnDrawDown;


/// 状态
- (void)initWithStatusLabel;

/// 订单号
- (void)initWithOrderNoLabel;

/// 缩略图
- (void)initWithThumalImgView;

/// 商品名称
- (void)initWithGoodsNameLabel;

/// 积分
- (void)initWithCntLabel;

/// 备注
- (void)initWithRemarkLabel;

/// 下单时间
- (void)initWithCreateTimeLabel;

/// 收货方式
- (void)initWithReceiveWayLabel;

/// 支付方式
- (void)initWithPaymentLabel;

@end

@implementation AEMyExchangeViewCell


- (void)initWithSubViewCell {
    [super initWithSubViewCell];
    
    [self initWithBgView];
}

- (void)initWithBgView {
    if (!_bgView) {
        CGRect frame = CGRectMake(10, 10, kScreenW - 20, 0);
        _bgView = [[UIView alloc] initWithFrame:frame];
        _bgView.backgroundColor = kWhiteColor;
        _bgView.layer.cornerRadius = 6;
        _bgView.layer.masksToBounds = YES;
        [self.contentView addSubview:_bgView];
    }
    if (self.model) {
        self.bgView.height = (self.model.receiveType == NO ? kAEMyExchangeViewCellHeight - 10 : kAEMyExchangeViewCellNoHeight - 10);
    }
}

/// 状态
- (void)initWithStatusLabel {
    if (!_statusLabel) {
        CGRect frame = CGRectMake(0, 15, 0, 22);
        _statusLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:13 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#FF9300"];
            label.textAlignment = NSTextAlignmentRight;
        }];
    }
    if (self.model) {
        UIColor *color = [UIColor colorWithHexString:@"#333333"];
        NSString *title = [AEStringExten localizedValueWithKey:@"已领取"];
        if (!self.model.receiveType) {
            color = [UIColor colorWithHexString:@"#FF9300"];
            title = [AEStringExten localizedValueWithKey:@"待领取"];
        }
        float width = [title widthForFont:self.statusLabel.font] + 2;
        _statusLabel.textColor = color;
        _statusLabel.text = title;
        _statusLabel.width = width;
        _statusLabel.left = self.bgView.width - 14 - width;
    }
}

/// 订单号
- (void)initWithOrderNoLabel {
    if (!_orderNoLabel) {
        CGRect frame = self.statusLabel.frame;
        frame.origin.x = 15;
        _orderNoLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:16 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
            
        }];
    }
    if (self.model) {
        self.orderNoLabel.width = self.statusLabel.left - 15 - 10;
        self.orderNoLabel.text = [NSString stringWithFormat:@"订单号：%@", objectNull(self.model.orderCode)];
    }
}

/// 缩略图
- (void)initWithThumalImgView {
    UIImage *image = [UIImage imageNamed:@"placeholder_image"];
    if (!_thumalImgView) {
        CGRect frame = CGRectMake(15, self.orderNoLabel.bottom + 14, 77, 77);
        _thumalImgView = [UIImageView createWithImageView:self.bgView frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
            imageView.layer.cornerRadius = 4;
            imageView.layer.masksToBounds = YES;
        }];
    }
    if (self.model) {
        [self.thumalImgView sd_setImageWithURL:[NSURL URLWithString:self.model.productUrl] placeholderImage:image completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            
        }];
    }
}

/// 商品名称
- (void)initWithGoodsNameLabel {
    if (!_goodsNameLabel) {
        CGRect frame = CGRectMake(self.thumalImgView.right + 10, self.thumalImgView.top, self.bgView.width - self.thumalImgView.right - 10 - 15, 22);
        _goodsNameLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:15 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
    if (self.model) {
        self.goodsNameLabel.text = objectNull(self.model.productName);
    }
}

/// 积分
- (void)initWithCntLabel {
    if (!_cntLabel) {
        CGRect frame = self.goodsNameLabel.frame;
        frame.size.height = 20;
        frame.origin.y = self.goodsNameLabel.bottom + 4;
        _cntLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#F6020A"];
        }];
    }
    if (self.model) {
        self.cntLabel.text = [NSString stringWithFormat:@"%zd积分", self.model.productScore];
    }
}

/// 备注
- (void)initWithRemarkLabel {
    if (!_remarkLabel) {
        CGRect frame = self.cntLabel.frame;
        frame.origin.y = self.cntLabel.bottom + 4;
        frame.size.height = 16;
        _remarkLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#999999"];
        }];
    }
    if (self.model) {
        self.remarkLabel.text = objectNull(self.model.remark);
    }
}

- (void)initWithOneLine {
    if (!_oneLine) {
        CGRect frame = CGRectMake(15, self.thumalImgView.bottom + 19, self.bgView.width - 30, 1);
        _oneLine = [CALayer drawLine:self.bgView frame:frame lineColor:[UIColor colorWithHexString:@"#F2F2F2"]];
    }
}

/// 下单时间
- (void)initWithCreateTimeLabel {
    if (!_createTimeLabel) {
        CGRect frame = CGRectMake(15, self.oneLine.bottom + 12, self.oneLine.width, 20);
        _createTimeLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#999999"];
        }];
    }
    if (self.model) {
        self.createTimeLabel.text = [NSString stringWithFormat:@"下单时间：%@", objectNull(self.model.orderTime)];
    }
}

/// 收货方式
- (void)initWithReceiveWayLabel {
    if (!_receiveWayLabel) {
        CGRect frame = self.createTimeLabel.frame;
        frame.origin.y = self.createTimeLabel.bottom + 6;
        _receiveWayLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#999999"];
        }];
    }
    if (self.model) {
        self.receiveWayLabel.text = [NSString stringWithFormat:@"收货方式：%@", @"用户请联系客服并领取"];
    }
}

/// 支付方式
- (void)initWithPaymentLabel {
    if (!_paymentLabel) {
        CGRect frame = self.receiveWayLabel.frame;
        frame.origin.y = self.receiveWayLabel.bottom + 6;
        _paymentLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#999999"];
        }];
    }
    if (self.model) {
        self.paymentLabel.text = [NSString stringWithFormat:@"支付方式：%@", @"积分支付"];
    }
}

- (void)initwithTwoLine {
    if (!_twoLine) {
        CGRect frame = self.oneLine.frame;
        frame.origin.y = self.paymentLabel.bottom + 13;
        _twoLine = [CALayer drawLine:self.bgView frame:frame lineColor:[UIColor colorWithHexString:@"#F2F2F2"]];
    }
    if (self.model) {
        if (!self.model.receiveType) {
            self.twoLine.hidden = NO;
        } else {
            self.twoLine.hidden = YES;
        }
    }
}

- (void)initWithBtnDrawDown {
    if (!_btnDrawDown) {
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
        NSString *title = [AEStringExten localizedValueWithKey:@"确认领取"];
        float width = [title widthForFont:font] + 2 + 40;
        CGRect frame = CGRectMake(self.bgView.width - 15 - width, self.twoLine.bottom + 16, width, 30);
        _btnDrawDown = [UIButton createWithButton:self.bgView frame:frame font:font target:self action:@selector(actionWithDrawDown:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
            button.backgroundColor = [UIColor colorWithHexString:@"#F6020A"];
            button.layer.cornerRadius = frame.size.height * 0.5;
            button.layer.masksToBounds = YES;
        }];
    }
    if (self.model) {
        if (!self.model.receiveType) {
            self.btnDrawDown.hidden = NO;
        } else {
            self.btnDrawDown.hidden = YES;
        }
    }
}

/// 去领取
/// - Parameter sender: sender description
- (void)actionWithDrawDown:(id)sender {
    if (self.touchDrawDownBlock) {
        self.touchDrawDownBlock();
    }
}

- (void)updateCellData:(id)cellEntity {
    self.model = cellEntity;
    
    [self initWithBgView];
    
    /// 状态
    [self initWithStatusLabel];

    /// 订单号
    [self initWithOrderNoLabel];

    /// 缩略图
    [self initWithThumalImgView];

    /// 商品名称
    [self initWithGoodsNameLabel];

    /// 积分
    [self initWithCntLabel];

    /// 备注
    [self initWithRemarkLabel];
    
    [self initWithOneLine];

    /// 下单时间
    [self initWithCreateTimeLabel];

    /// 收货方式
    [self initWithReceiveWayLabel];

    /// 支付方式
    [self initWithPaymentLabel];
    
    [self initwithTwoLine];
    
    [self initWithBtnDrawDown];
}

@end
